import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_demo/constants/constants.dart';
import 'package:flutter_app_demo/utils/NetUtils.dart';
import 'package:flutter_app_demo/utils/data_utils.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

class NewsDetailPage extends StatefulWidget {
  final int id;

  NewsDetailPage({this.id}) : assert(id != null);

  @override
  _NewsDetailPageState createState() => _NewsDetailPageState();
}

class _NewsDetailPageState extends State<NewsDetailPage> {
  FlutterWebviewPlugin _flutterWebviewPlugin = FlutterWebviewPlugin();

  bool isLoading = true;
  String url;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    //监听url变化
    //四种状态
    //WebViewState.startLoad
    //WebViewState.shouldStart
    //WebViewState.abortLoad
    //WebViewState.finishLoad
    _flutterWebviewPlugin.onStateChanged.listen((state) {
      print("LoginWebPage onStateChanged = $state");
      if (state.type == WebViewState.finishLoad) {
        if (mounted) {
          setState(() {
            isLoading = false;
          });
        }
      } else if (state.type == WebViewState.startLoad) {
        if (mounted) {
          setState(() {
            isLoading = true;
          });
        }
      }
    });

    DataUtils.getAccessToken().then((token) {
      if (token == null || token.length == 0) {
        return;
      }

      //封装参数
      Map<String, dynamic> params = {
        "access_token": token,
        "id": widget.id,
        "dataType": "json",
      };

      NetUtils.get(AppUrls.NEWS_DETAIL, params).then((data) {
        //print(data);
        if (data != null && data.isNotEmpty) {
          //解析gson数据
          Map<String, dynamic> map = json.decode(data);
          print("map  = ${map.length}");
          print("map  = $map");

          if (!mounted) {
            return;
          }
          setState(() {
            url = map["url"];
          });
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> _appBarTitle = [
      Text(
        "咨询详情",
        style: TextStyle(
          color: Color(AppColors.APPBAR),
        ),
      ),
    ];

    if (isLoading) {
      _appBarTitle.add(SizedBox(
        width: 10,
      ));

      //加载图表
      _appBarTitle.add(CupertinoActivityIndicator());
    }

    return url == null
        ? Scaffold(
            appBar: AppBar(
              title: Text(
                "咨询详情",
                style: TextStyle(
                  color: Color(AppColors.APPBAR),
                ),
              ),
              iconTheme: IconThemeData(color: Color(AppColors.APPBAR)),
            ),
            body: Center(
              child: CupertinoActivityIndicator(),
            ),
          )
        : WebviewScaffold(
            url: url,
            appBar: AppBar(
              title: Row(
                children: _appBarTitle,
              ),
              iconTheme: IconThemeData(color: Color(AppColors.APPBAR)),
            ),
            //允许执行js
            withJavascript: true,
            //允许本地存储
            withLocalStorage: true,
            //允许缩放
            withZoom: true,
          );
  }
}
